<template>
    <!-- 人员管理-奖惩记录 -->
    <div class="empReward">
        <div class="top">
            <div class="search">
                <div class="list">
                    <DatePicker
                        type="daterange"
                        :clearable="false"
                        placeholder="开始时间   -   结束时间"
                        style="width: 200px"
                        v-model="value"
                        @on-change="getTime"
                    ></DatePicker>
                </div>
                <div class="list">
                    <Input placeholder="人员姓名搜索" style="width:150px" v-model="keyword" clearable />
                    <input type="password" autocomplete="new-password" style="display: none" />
                </div>
                <input type="text" class="intBox" style="display:none" />
                <div class="list" @click="onSerch">
                    <Button type="primary">搜索</Button>
                </div>
                <div class="list" @click="onReset">
                    <Button>重置</Button>
                </div>
            </div>
            <div class="down">
                <el-button
                    type="primary"
                    @click="updateRow(0)"
                    size="small"
                    plain
                    :disabled="!btnList.includes('emp/reward/add')"
                    >添加奖惩记录</el-button
                >
            </div>
        </div>
        <div class="bottom">
            <div class="table">
                <el-table :data="list" :height="tableHeight" :stripe="true">
                    <el-table-column prop="empName" label="人员姓名"></el-table-column>
                    <el-table-column prop="type" label="奖惩类型">
                        <template slot-scope="scope">
                            <el-tag type="success" v-if="scope.row.type == 1" size="small">奖励</el-tag>
                            <el-tag type="info" v-if="scope.row.type == 2" size="small">惩罚</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="item" label="奖惩记录分类">
                        <template slot-scope="scope">
                            <el-tag type="success" size="small">{{ labelByValue[scope.row.item] }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="description" label="描述"></el-table-column>
                    <el-table-column prop="date" label="发生时间"></el-table-column>
                    <el-table-column label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button
                                @click.native.prevent="remove(scope.$index, list)"
                                type="text"
                                size="small"
                                style="color:red"
                                :disabled="!btnList.includes('emp/reward/delete')"
                                >删除</el-button
                            >
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="clearfix"  >
                <div class="float-right">
                    <Page
                        v-if="totalCount"
                        style="margin: 20px 0;text-align: center;"
                        :current.sync="current"
                        :total="totalCount"
                        :page-size="pageSize"
                        show-elevator
                        show-total
                        show-sizer
                        @on-change="pageChange"
                        @on-page-size-change="pageSizeChange"
                    ></Page>
                </div>
            </div>
        </div>

        <Modal v-model="deleteModal" title="删除" width="360px;" :mask-closable="false">
            <div style="margin:10px">确定要删除这条数据吗？</div>
            <div slot="footer">
                <el-button @click="cancel(2)" type="info" size="small" plain>取消</el-button>
                <el-button @click="detele" type="primary" size="small" plain v-preventReClick="1500">确定</el-button>
            </div>
        </Modal>

        <Modal v-model="updateModal" title="添加奖惩记录" width="520px" :mask-closable="false">
            <div class="item">
                <span class="tip">*</span>
                <span>人员:</span>
                <Select v-model="empId" style="width:300px" placeholder="请选择人员" :filterable="true" @on-change="getEmpType">
                    <Option v-for="item in empList" :value="item.empId" :key="item.empId" >{{ item.empName }}</Option>
                </Select>
            </div>
            <div class="item">
                <span class="tip">*</span>
                <span>发生时间:</span>
                <DatePicker
                    type="date"
                    v-model="date"
                    placeholder="请选择发生时间"
                    style="width: 300px"
                    @on-change="getDate"
                ></DatePicker>
            </div>
            <div class="item">
                <span>奖惩类型:</span>
                <div class="radio">
                    <el-radio-group v-model="radio">
                        <el-radio :label="1">奖励</el-radio>
                        <el-radio :label="2">惩罚</el-radio>
                    </el-radio-group>
                </div>
            </div>
            <div class="item">
                <span class="tip">*</span>
                <span>奖惩分类:</span>
                <Select v-model="itemId" style="width:300px" placeholder="请选择奖惩分类" :filterable="true">
                    <Option v-for="item in itemList" :value="item.code" :key="item.code">{{ item.name }}</Option>
                </Select>
            </div>
            <div class="item">
                <span class="tip">*</span>
                <span>描述:</span>
                <Input v-model="description" type="textarea" placeholder="请输入描述内容" :maxlength="200" style="width: 300px" show-word-limit />
            </div>
            <div slot="footer">
                <el-button @click="cancel(1)" type="info" size="small" plain>取消</el-button>
                <el-button @click="submitAll" type="primary" size="small" plain v-preventReClick="1500">确定</el-button>
            </div>
        </Modal>
    </div>
</template>

<script>

import {
    getEmpRewardList,
    getEmpRewardAdd,
    getEmpRewardDel,
    getAllPersonnelList,
    getSelectValue
} from "@/api/api.js";
export default {
    data() {
        return {
            current: 1,
            pageSize: 10,
            pageNum: 1,
            totalCount: "",
            corpType: "",
            projectId: "",
            keyword: "",
            list: [],
            corpList: [],
            updateModal: false,
            isAdd: true,
            removeId: "",
            deleteModal: false,
            btnList: [],
            deptName: "",
            startDate: "",
            endDate: "",
            value: [],
            empList: [],
            empId: "",
            empName: "",
            date: "",
            tableHeight: window.innerHeight - 355,
            itemList: [], //改动 下拉值
            itemId: "",
            radio: "",
            description: "",
            labelByValue: {}
        };
    },
    computed: {
        porjectObject() {
            return this.$store.state.porjectObject;
        }
    },
    watch: {
        porjectObject(val, oldVal) {
            if (val.deptId != oldVal.deptId) {
                this.projectId = val.deptId;
                this.deptName = val.name;
                this.pageNum = 1;
                this.current = 1;
                this.keyword = "";
                this.value = [];
                this.getAllList();
                this.getEmpList();
            }
        }
    },
    methods: {
        //获取所有人员
        getEmpList() {
            let params = {
                deptId: this.projectId
            };
            getAllPersonnelList(params)
                .then(res => {
                    if (res.code == 200) {
                        this.empList = res.data.list;
                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },
        getAllList() {
            let params = {
                pageSize: this.pageSize,
                pageNum: this.pageNum,
                keyword: this.keyword,
                projectId: this.projectId,
                startDate: this.startDate,
                endDate: this.endDate
            };
            getEmpRewardList(params)
                .then(res => {
                    if (res.code == 200) {
                        this.list = res.data.list;
                        this.totalCount = res.data.total;
                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },
        getTime(value) {
            this.startDate = value[0];
            this.endDate = value[1];
            // this.pageNum = 1;
            // this.current = 1;
            // this.getAllList();
        },
        // onSearch() {
        //     this.pageNum = 1;
        //     this.current = 1;
        //     this.getAllList();
        // },
        
        onSerch() {
            this.pageNum = 1;
            this.current = 1;
            this.getAllList();
        },
        onReset(){
            this.value = []
            this.startDate = null
            this.endDate = null
            this.keyword = null
            this.pageNum = 1;
            this.current = 1;
            this.getAllList();
        },
        pageChange(index) {
            this.pageNum = index;
            this.getAllList();
        },
        pageSizeChange(index) {
            this.pageSize = index;
            this.getAllList();
        },
        updateRow() {
            this.updateModal = true;
            this.empId = "";
            this.empName = "";
            this.date = "";
            this.radio = 1;
            this.itemId = "";
            this.description = "";
        },
        getEmpType(index) {
            this.empId = index;
            this.empList.forEach(i => {
                if (index == i.empId) {
                    this.empName = i.empName;
                }
            });
        },
        getDate(value) {
            this.date = value;
        },
        submitAll() {
            if (this.empId == "") {
                this.$Modal.warning({
                    title: "您有必填字段",
                    content: "请选择人员"
                });
                return;
            }
            if (this.date == "") {
                this.$Modal.warning({
                    title: "您有必填字段",
                    content: "请选择发生时间"
                });
                return;
            }
            if (this.itemId == "") {
                this.$Modal.warning({
                    title: "您有必填字段",
                    content: "请选择奖惩分类"
                });
                return;
            }
            if (this.description == "") {
                this.$Modal.warning({
                    title: "您有必填字段",
                    content: "请输入描述"
                });
                return;
            }
            let params = {
                empId: this.empId,
                empName: this.empName,
                type: this.radio,
                item: this.itemId,
                description: this.description,
                date: this.date
            };
            getEmpRewardAdd(params)
                .then(res => {
                    if (res.code == 200) {
                        this.$Message.success("操作成功");
                        this.updateModal = false;
                        this.getAllList();
                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },
        remove(index, rows) {
            this.removeId = rows[index].id;
            this.deleteModal = true;
        },
        detele() {
            getEmpRewardDel(this.removeId)
                .then(res => {
                    if (res.code == 200) {
                        this.$Message.success("操作成功");
                        this.getAllList();
                        this.deleteModal = false;
                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },

        cancel(type) {
            if (type == 1) {
                this.updateModal = false;
            } else if (type == 2) {
                this.deleteModal = false;
            }
        },
        //改动
        selectValue() {
            let params = {
                code: "reward_type"
            };
            const _this = this;
            getSelectValue(params)
                .then(res => {
                    if (res.code == 200) {
                        this.itemList = res.data.childList;
                        let province_list = {};
                        for (let i = 0; i < this.itemList.length; i++) {
                            province_list[this.itemList[i].code] = this.itemList[i].name;
                        }
                        this.labelByValue = province_list;
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },
    },
    created() {
        this.selectValue(); //改动 初次加载下拉数据
    },
    mounted() {
        this.projectId = this.$store.state.porjectObject.deptId;
        this.deptName = this.$store.state.porjectObject.name;
        this.getAllList();
        this.getEmpList();
        this.btnList = JSON.parse(localStorage.getItem("arr"));
    }
};
</script>

<style scoped lang="less">
// @import "./../../assets/style/emp_common.less";
@import "./../../assets/style/themeBase.less";
.empReward {
    width: 100%;
    height: 100%;
    padding: 10px;
    box-sizing: border-box;
    background: #f5f5f5;
    .top {
        width: 100%;
        height: 72px;
        background: #fff;
        border-radius: 10px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .search {
            width: auto;
            margin-left: 20px;
            padding-top: 20px;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            .list {
                display: flex;
                justify-content: flex-start;
                align-items: center;
                padding-bottom: 20px;
                margin-right: 20px;
                span {
                    width: 70px;
                }
            }
        }
        .down {
            width: auto;
            margin-right: 20px;
        }
    }
    .bottom {
        width: 100%;
        padding: 20px;
        margin-top: 10px;
        box-sizing: border-box;
        height: calc(100% - 82px);
        background: #fff;
        border-radius: 10px;
        .down {
            width: 98%;
            margin: 0 auto;
            margin-top: 20px;
        }
        .msg {
            margin-bottom: 20px;
            display: flex;
            flex-wrap: wrap;
            .list {
                display: flex;
                justify-content: flex-start;
                align-items: center;
                margin-right: 24px;
                p {
                    margin-right: 10px;
                    font-size: 16px;
                    color: #000;
                    &:nth-child(2) {
                        color: red;
                    }
                }
            }
        }
        .updateCorp {
            cursor: pointer;
            margin-left: 5px;
            padding: 2px 4px;
            font-size: 12px;
            font-weight: 400;
            line-height: 1;
            color: #ffffff;
            border-radius: 3px;
            background-color: var(--themeColor);
        }
    }
}
.item {
    width: 400px;
    display: flex;
    margin: 0 auto;
    justify-content: space-between;
    align-items: center;
    margin-top: 10px;
    position: relative;
    margin-bottom: 20px;
    padding-left: 16px;
    box-sizing: border-box;
    .radio {
        width: 300px;
        display: flex;
        justify-content: center;
    }
    .tip {
        position: absolute;
        top:8px;
        left:0;
        color: red;
        font-size: 16px;
    }
}
// /deep/.ivu-icon-ios-close{
//     color: #fff;
//     &:hover{
//         color: #fff;
//     }
// }



/deep/.ivu-input-group{
    border-radius:20px;
    }
    /deep/.ivu-input-group .ivu-input{
        border-radius: 20px 0 0 20px;
        border-color:#EBF2F8;
        background:#EBF2F8;
    }
    /deep/.ivu-input-group-append{
        border-radius: 0 20px 20px 0;
    }
    /deep/.el-tag.el-tag--success{
        background-color: #E7F8F2;
        border-color: #28D79C;
        color: #3FCF77;
    }
    /deep/.el-tag{
        border-radius: 2px;
    }
    /deep/.ivu-select-selection{
        border-radius: 2px;
    }
    /deep/.el-tag.el-tag--info{
    background-color:rgba(255, 72, 72, 0.1);
    border-radius:2px;
    border-color:#FF4848;
    color:#FF4848;
}
</style>
